#! /usr/bin/env node

const fs = require("fs-extra");
const path = require("path");
const spawn = require("cross-spawn");

/**
 * 启动组件
 * @param {string} compName 组件名称
 */
const serveComp = (compName) => {
  // 预览的vue文件目录
  const previewFileDir = path.join(__dirname, "../src/views/develop/index.vue");
  // 修改预览的组件名称
  fs.writeFileSync(
    path.join(__dirname, "../.env.development.local"),
    `VITE_APP_COMP_NAME=${compName}`
  );

  // 启动服务
  spawn("vite", ["serve"], {
    stdio: "inherit",
  });
}

(() => {
  // 获取命令行组件参数
  const compName = process.argv[process.argv.length - 1];
  // 获取packages下的文件夹名称
  const packagesDir = fs.readdirSync(path.join(__dirname, "../src/packages"));
  console.log(222, compName, packagesDir);
  // 如果packages下包含该组件，直接启动
  if (packagesDir.includes(compName)) {
    serveComp(compName);
  } else {
    // serveCompByInputCompName(packagesDir);
  }
})();
